package com.xiaxueqi.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.dao.Page;
import com.niagara.service.base.BaseService;
import com.xiaxueqi.entity.book.BookForsale;
import com.xiaxueqi.entity.book.BookForsaleExt;
import com.xiaxueqi.entity.book.BookStore;
import com.xiaxueqi.service.BookDTOService;

public class BookDTOServiceImpl implements BookDTOService {

	@Autowired
	private BaseService baseService;

	public BookForsaleExt getBookDTO(BookForsale bookForsale) throws Exception {
		BookForsaleExt book = new BookForsaleExt();
		if (bookForsale != null && bookForsale.getBookId() != null) {
			BookStore bookStore = this.baseService.get(BookStore.class,
					bookForsale.getBookId());
			// 相同的字段名就取bookForsale中的
			Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
			mapper.map(bookStore, book);
			mapper.map(bookForsale, book);
			book.setOrigPrice(bookStore.getPrice());
			if (book.getImgPath() == null) {
				book.setImgPath(bookStore.getImgPath());
			}
			book.setOrigPrice(bookStore.getPrice());
		}
		return book;
	}

	public BookForsaleExt getBookDTOById(Integer bookForsaleId)
			throws Exception {
		BookForsale bookForsale = this.baseService.get(BookForsale.class,
				bookForsaleId);
		BookForsaleExt book = this.getBookDTO(bookForsale);
		return book;
	}

	public List<BookForsaleExt> getBookDTOList(List<BookForsale> bookForsaleList)
			throws Exception {
		List<BookForsaleExt> bookList = new ArrayList<BookForsaleExt>();
		if (bookForsaleList != null && bookForsaleList.size() != 0) {
			for (BookForsale bookForsale : bookForsaleList) {
				BookForsaleExt book = this.getBookDTO(bookForsale);
				bookList.add(book);
			}
		}
		return bookList;
	}

	public Page<BookForsaleExt> getBookDTOPage(Page<BookForsale> bookForsalePage)
			throws Exception {
		if (bookForsalePage != null) {
			List<BookForsaleExt> bookList = this.getBookDTOList(bookForsalePage
					.getPageList());
			int currentPage = bookForsalePage.getCurrentPage();
			int pageSize = bookForsalePage.getPageSize();
			int totalCount = bookForsalePage.getTotalCount();

			Page<BookForsaleExt> bookPage = new Page<BookForsaleExt>(
					currentPage, pageSize, totalCount, bookList);
			return bookPage;
		} else
			return null;
	}

}
